package com.example.listviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Fruit> fruitList = new ArrayList<>();

   // private String[] data = { "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple",
    //        "Strawberry", "Strawberry", "Cherry", "Mange", "Apple", "Banana", "Orange", "Watermelon",
    //        "Pear", "Grape", "Pineapple", "Strawberry", "Strawberry", "Cherry", "Mange"};

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();
        FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }

    private void initFruits() {
        for (int i = 0; i < 4; i++) {
            Fruit apple = new Fruit("Apple", R.mipmap.caolubeijing);
            fruitList.add(apple);
            Fruit banana = new Fruit("Banana", R.mipmap.caolubeijing);
            fruitList.add(banana);
            Fruit orange = new Fruit("Orange", R.mipmap.caolubeijing);
            fruitList.add(orange);
            Fruit watermelon = new Fruit("Watermelon", R.mipmap.caolubeijing);
            fruitList.add(watermelon);
        }
    }
}
